package service.impl;

import library.service.Book;

public class MixedBookImpl implements Book {
	private String name;
	private String category;
	private static String MIXED_CATEGORY = "Technical";


	public void setName(String name) {
		this.name = name;
	}

	// --- Constructors, Getters, Setters --------------------------------------
	public MixedBookImpl() {
		category = MIXED_CATEGORY;
	}

	public MixedBookImpl(String name) {
		this();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String getCategory() {
		return category;
	}


	// --- Overriden methods ---------------------------------------------------
	public int compareTo(Object object) {
		if (object == null || !(object instanceof Book)) {
			throw new IllegalArgumentException(
					"Incorrect object for comparison");
		}

		Book book = (Book) object;
		int bookNameComparison = getName().compareTo(book.getName());
		if (bookNameComparison != 0) {
			return getCategory().compareTo(book.getCategory());
		} else {
			return bookNameComparison;
		}
	}

	public boolean equals(Object object) {
		// System.out.println("MixedBookImpl equals() executed");
		if (object == null || !(object instanceof Book)) {
			throw new IllegalArgumentException(
					"Incorrect input object during test for objects equality");
		}

		Book book = (Book) object;

		return name.equals(book.getName())
				&& category.equalsIgnoreCase(MIXED_CATEGORY);
	}

	public int hashCode() {
		char firstCharInBookName = name.charAt(0);
		return MIXED_CATEGORY.length() + ((int) firstCharInBookName);
	}

	public String toString() {
		StringBuffer stringBuffer = new StringBuffer(30);
		stringBuffer.append("Name: ").append(getName()).append(", ");
		stringBuffer.append("Category: ").append(getCategory());
		return stringBuffer.toString();
	}

}
